package anagramUtils;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class AnagramFinder {

    public HashMap<String, List<String>> parse(String[] words) {
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();

        for (String word1 : words) {
            String word = word1.trim().toLowerCase();
            char[] letters = word.toCharArray();
            Arrays.sort(letters);
            String possibleKey = new String(letters);
            List<String> anagrams;

            if (map.containsKey(possibleKey)) {
                anagrams = map.get(possibleKey);
            } else {
                anagrams = new ArrayList<String>();
            }

            if (!anagrams.contains(word)) {
                anagrams.add(word);
            }

            map.put(possibleKey, anagrams);
        }
        return map;

    }
}
